package com.yxzx.ebs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxzx.common.EduException;
import com.yxzx.common.ResultCode;
import com.yxzx.ebs.client.VodClient;
import com.yxzx.ebs.entity.EbsVideo;
import com.yxzx.ebs.entity.vo.EbsVideoInfoForm;
import com.yxzx.ebs.mapper.EbsVideoMapper;
import com.yxzx.ebs.service.EbsVideoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author kongfuzi
 * @since 2020-07-31
 */
@Service
public class EbsVideoServiceImpl extends ServiceImpl<EbsVideoMapper, EbsVideo> implements EbsVideoService {

    /**
     * VideoService 的客户端
     */
    @Autowired
    private VodClient vodClient;
    @Override
    public boolean removeVideoById(String id) {
        //1.查询阿里云视频id
        EbsVideo ebsVideo = baseMapper.selectById(id);
        String videoSourceId = ebsVideo.getVideoSourceId();
        //2.删除视频资源
        if(!StringUtils.isEmpty(videoSourceId)){
            vodClient.removeVideo(videoSourceId);
        }
        //3.删除小节数据
        int result = baseMapper.deleteById(id);
        return result > 0;
    }

    @Override
    public boolean removeByCourseId(String courseId) {
        //1.根据课程id查询所有的视频列表
        QueryWrapper<EbsVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        queryWrapper.select("video_source_id");
        List<EbsVideo> videoList = baseMapper.selectList(queryWrapper);
        //2.获取所有视频列表的云端原始视频ID
        List<String> videoIds = videoList.stream().map(EbsVideo::getVideoSourceId).collect(Collectors.toList());
        //3.调用远程视频服务的方法
        if (videoIds != null && videoIds.size()>0) {
            vodClient.removeVideoList(videoIds);
        }
        //4.删除video表的记录
        QueryWrapper<EbsVideo> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id",courseId);
        int i = baseMapper.delete(videoQueryWrapper);
        return i > 0 ;
    }

    @Override
    public boolean existsVideo(String chatperId) {
        QueryWrapper<EbsVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id",chatperId);
        Integer integer = baseMapper.selectCount(queryWrapper);
        return integer != null && integer > 0;
    }

    @Override
    public void saveVideoInfo(EbsVideoInfoForm videoInfoForm) {
        EbsVideo ebsVideo = new EbsVideo();
        BeanUtils.copyProperties(videoInfoForm,ebsVideo);
        boolean save = this.save(ebsVideo);
        if (!save){
            throw new EduException(ResultCode.ERROR,"保存课时信息失败");
        }
    }

    @Override
    public EbsVideoInfoForm getVedioInfoById(String id) {
        EbsVideo ebsVideo = this.getById(id);
        EbsVideoInfoForm form = new EbsVideoInfoForm();
        BeanUtils.copyProperties(ebsVideo,form);
        return form;
    }

    @Override
    public void updateVideoInfo(EbsVideoInfoForm videoInfoForm) {
        EbsVideo video = new EbsVideo();
        BeanUtils.copyProperties(videoInfoForm,video);
        boolean b = this.updateById(video);
        if(!b){
            throw new EduException(ResultCode.ERROR,"更新课时信息失败");
        }
    }
}
